//
//  SHXEntertainmentDetailDataHepler.m
//  SHXEntertainment
//
//  Created by lanou3g on 15/8/15.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "SHXEntertainmentDetailDataHepler.h"
#import "JQnewsModel.h"
#import "Url.h"
#import <UIKit/UIKit.h>

#define kScreenWidth ([UIScreen mainScreen].bounds.size.width)
#define kScreenHeight ([UIScreen mainScreen].bounds.size.height)

@interface SHXEntertainmentDetailDataHepler ()

@property(nonatomic,strong)NSMutableArray *photosArray;
@property(nonatomic,copy)NSString *body;
@property(nonatomic,strong)NSArray *videoArray;
@property(nonatomic,copy)NSString *s;
@property(nonatomic,copy)NSString *html;
@end
@implementation SHXEntertainmentDetailDataHepler
+(instancetype)sharedSHXEnDetail
{
    static SHXEntertainmentDetailDataHepler *detailHelper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        detailHelper = [[SHXEntertainmentDetailDataHepler alloc] init];
    });
    
    return detailHelper;
}

-(void)getDataFromWebWithString:(JQnewsModel *)model
{
    
    
    if (model.photosetID == nil) {
        
        self.body = nil;
        NSString *string = [kEntertainmentDetailUrl stringByAppendingString:model.docid];
        NSString *strin = [string stringByAppendingString:@"/full.html"];
        
//        NSLog(@"%@",strin);
        
        NSDictionary *dict = [self webDataWithStr:strin];
        
        //解析数据
        NSDictionary *secondDic = [dict objectForKey:model.docid];
        
         self.html = [secondDic objectForKey:@"body"];
        
//        NSLog(@"%@",self.body);
        
        // NSString *digest = [secondDic objectForKey:@"digest"];
        
        NSString *ptime = [secondDic objectForKey:@"ptime"];
        NSString *source = [secondDic objectForKey:@"source"];
        NSString *str1 = @"来源:";
        NSString *strr = [str1 stringByAppendingString:source];
        self.sourcestring = [strr stringByAppendingString:ptime];
        self.title = [secondDic objectForKey:@"title"];
    
        
        NSArray *imgArray = [secondDic objectForKey:@"img"];
        
        for (NSDictionary *smalDic in imgArray) {
            NSString *daxiao = [smalDic objectForKey:@"pixel"];
            NSArray *w_h = [daxiao componentsSeparatedByString:@"*"];
            //            NSLog(@"%@, %@", w_h[0], w_h[1]);
            
                
         self.s = [NSString stringWithFormat:@"<img src=\"%@\" width=\"%f\" height=\"%f\"></p><p><font size=\"2px\"color=#A8A8A8>%@</font></p>", smalDic[@"src"], kScreenWidth - 20, [w_h[1] floatValue] / ([w_h[0] floatValue]/kScreenWidth),smalDic[@"alt"]];
                
            if (smalDic[@"photosetID"]) {
                 self.s = [NSString stringWithFormat:@"<p><img src=\"%@\" width=\"%lf\" height=\"%lf\" style=\"positon:absolute;\"><img src=\"/Users/zhangxiaoqiang/Documents/Me/News/ZXQNews/ZXQNews/Classes/Game/Resources/photoset@2x.png\" width=\"45\" height=\"30\" style=\"position:relative; left:0px;top:-30px;\"><font size=\"2px\"color=#A8A8A8>%@</font></p>", smalDic[@"src"],  kScreenWidth - 20, [w_h[1] floatValue] / ([w_h[0] floatValue]/kScreenWidth), smalDic[@"alt"]];
                
            }
             
            self.html = [self.html stringByReplacingOccurrencesOfString:smalDic[@"ref"] withString:self.s];
        }
        
//        self.videoArray = [secondDic objectForKey:@"video"];
        
//        if (self.videoArray.count > 0) {
        
//            for (NSDictionary *Dic in self.videoArray) {
                
//                NSString *string = [Dic objectForKey:@"url_mp4"];
//                NSLog(@" video ......%@",string);
                
//                ZXQMoviePlayer *player = [[ZXQMoviePlayer alloc] initWithContentURL:[NSURL URLWithString:string]];
//                
//                
//                
//                NSString *ss = [NSString stringWithFormat:@"<p><video control poster=\"%@\"src=\"%@\" width=\"%f\"",Dic[@"cover"],Dic[@"url_mp4"],kScreenWidth - 20];
                
  //              self.html = [self.html stringByReplacingOccurrencesOfString:Dic[@"ref"] withString:ss];
                
               
                //[self presentMoviePlayerViewControllerAnimated:player];
                
   //         }
        
        
        //完全布局
        self.body = [NSString stringWithFormat:@"<p><font size=\"5px\">%@</font></p><p><font size=\"2px\"color=#A9A9A9>%@\n</font><font size=\"2px\"color=#A9A9A9>%@</font></p> <div class  = \"text\" style = \"text-align:left; padding-left:2px;\">%@</div>", model.title,model.source,model.ptime,self.html];
        //    NSLog(@"%@", HtmlString);
        
        // 详情页有些时候出现分割线，但是body中的分割线太长，导致webView因为内容太长，可以水平滚动了。所以替换成----
        self.body = [self.body stringByReplacingOccurrencesOfString:@"<p>　　—————————————————————————————————————————</p>" withString:@"<p>-------------------------------------------------------------------</p>"];

        if (_block) {
            _block(self.body);
        } else {
//            NSLog(@"Block is null");
        }
            
    }else
    {
//        [self.photosArray removeAllObjects];
//        NSString *str = [model.photosetID substringFromIndex:4];
//        
//        NSString *strr = [str substringFromIndex:5];
//       
//        NSString *strr1 = [str substringToIndex:4];
//        
//        NSString *photoStr = [NSString stringWithFormat:@"%@/%@",strr1,strr];
//        NSString *url = [KEnPotosetUrl stringByAppendingString:photoStr];
//        url = [url stringByAppendingString:@".json"];
//        
//        NSDictionary *dic = [self webDataWithStr:url];
//        
//        NSArray *array = [dic objectForKey:@"photos"];
//        
//        
//        for (NSDictionary *dic in array) {
//            
//            MJPhoto *photo = [[MJPhoto alloc] init];
//            photo.url = [NSURL URLWithString:dic[@"imgurl"]];
//            photo.desc = dic[@"imgtitle"];
//            
//            [self.photosArray addObject:photo];
//            
//            NSLog(@"....%@",self.photosArray);
//        }
    }
}


-(NSDictionary *)webDataWithStr:(NSString *)str
{
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
//    NSLog(@"%@",data);
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    return dict;
}

    
    
    
    
    
-(NSMutableArray *)photosArray
{
    if (!_photosArray) {
        self.photosArray = [NSMutableArray array];
    }
    
    return _photosArray;
}
-(NSString *)getHtmlString
{
    return self.body;
}

-(NSMutableArray *)allPhotosArray
{
    return self.photosArray;
}

-(NSArray *)getViediosArray
{
    return self.videoArray;
}
@end
